//
//  RegViewController.m
//  Manage
//
//  Created by 杨康 on 2018/1/9.
//  Copyright © 2018年 杨康. All rights reserved.
//

#import "RegViewController.h"
#import "QCCountdownButton.h"
@interface RegViewController ()
{
    YKTextField *_phoneTF;
    YKTextField *_passTF;
    QCCountdownButton *_codeBtn;
}
@end

@implementation RegViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"注册";
    
    //手机号
    YKTextField *phoneTF=[SingLeton myTextFieldsecure:NO andkeyboardType:UIKeyboardTypeNumberPad andplaceholder:@"输入手机号" andLeftImage:@"手机号" RequestSuccess:^(YKTextField *textField, NSRange range, NSString *string) {
        
        
        
        NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if (text.length==11) {
            //获取验证码按钮变蓝，可点击
            _codeBtn.enabled=YES;
            
        }else
        {
            //获取验证码按钮变灰，不可点击
            _codeBtn.enabled=NO;
        }
        
        
    }];
    _phoneTF=phoneTF;
    [self.view addSubview:phoneTF];
    
    [phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(40+TOP_HEIGHT);
        make.left.mas_equalTo(35);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-70, 44));
    }];
    phoneTF.backgroundColor=[UIColor colorWithHexString:@"f5f5f5"];
    
    //验证码
    YKTextField *verifyCodeTF=[SingLeton myTextFieldsecure:NO andkeyboardType:UIKeyboardTypeDefault andplaceholder:@"输入手机验证码" andLeftImage:@"验证码" RequestSuccess:^(YKTextField *textField, NSRange range, NSString *string) {
        
    }];
    [self.view addSubview:verifyCodeTF];
    
    [verifyCodeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(phoneTF.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(phoneTF);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-70, 44));
    }];
    verifyCodeTF.backgroundColor=[UIColor colorWithHexString:@"f5f5f5"];
    verifyCodeTF.clearButtonMode = UITextFieldViewModeNever;
    
    //获取验证码按钮
    QCCountdownButton *btn = [QCCountdownButton countdownButton];
    btn.title = @"获取验证码";
    _codeBtn=btn;
    //开始不可点击
    _codeBtn.enabled=NO;
    
    // 倒计时的时长
    btn.totalSecond = 120;
    // 字体
    btn.titleLabelFont = [UIFont systemFontOfSize:14];
    
    [verifyCodeTF addSubview:btn];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(90,30));
        make.centerY.equalTo(verifyCodeTF);
        make.right.equalTo(verifyCodeTF.mas_right).mas_offset(-10);
    }];
    //进度
    [btn onStartBlock:^{
        [self.view endEditing:YES];
        [self judgeCondition:btn];
    }processBlock:^(NSUInteger second){
        //输入框不能操作
        _phoneTF.enabled=NO;
        btn.title = [NSString stringWithFormat:@"剩余%li秒", (unsigned long)second] ;
    } onFinishedBlock:^{
        //输入框能操作
        _phoneTF.enabled=YES;
        // 倒计时完毕
        btn.title = @"重新获取";
    }];
    
    
    //登录密码
    YKTextField *passwordTF=[SingLeton myTextFieldsecure:YES andkeyboardType:UIKeyboardTypeDefault andplaceholder:@"输入密码" andLeftImage:@"密码" RequestSuccess:^(YKTextField *textField, NSRange range, NSString *string) {
    }];
    
    [self.view addSubview:passwordTF];
    [passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(verifyCodeTF.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(35);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-70, 44));
    }];
    _passTF=passwordTF;
    passwordTF.backgroundColor=[UIColor colorWithHexString:@"f5f5f5"];
    passwordTF.clearButtonMode = UITextFieldViewModeNever;
    
    UIButton *showButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:showButton];
    [showButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(passwordTF);
        make.right.equalTo(passwordTF.mas_right).mas_offset(-15);
    }];
    [showButton setImage:[UIImage imageNamed:@"密码隐藏"] forState:UIControlStateNormal];
    [showButton setImage:[UIImage imageNamed:@"密码显示"] forState:UIControlStateSelected];
    [showButton addTarget:self action:@selector(showOrHidden:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    //邀请码
    YKTextField *inviteTF=[SingLeton myTextFieldsecure:NO andkeyboardType:UIKeyboardTypeDefault andplaceholder:@"邀请码(选填)" andLeftImage:@"邀请码" RequestSuccess:^(YKTextField *textField, NSRange range, NSString *string) {
        
    }];
    [self.view addSubview:inviteTF];
    
    [inviteTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(passwordTF.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(passwordTF);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-70, 44));
    }];
    inviteTF.backgroundColor=[UIColor colorWithHexString:@"f5f5f5"];
    
    
    
    
    
    
    //确定按钮
    UIButton  *confirmButton=[SingLeton endButton:[UIColor colorWithHexString:@"09be00"] andTitle:@"注册" RequestSuccess:^(UIButton *btn) {
        
    }];
    [self.view addSubview:confirmButton];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(inviteTF.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(inviteTF);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-70, 44));
    }];
    
    
    
    NSString *str0=@"注册即表示同意";
    NSString *str1=@"“熊猫充电用户协议”";
    
    
    UIButton *agreement=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:agreement];
    [agreement mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(confirmButton.mas_bottom).mas_offset(15);
        
    }];
    [agreement setAttributedTitle:[NetworkPath setRichText:str0 withFont:14 withColor:[UIColor color3] andOtherText:str1 withFont:14 withColor:[UIColor colorWithHexString:@"09be00"] atIndex:7] forState:UIControlStateNormal];
    
    
    
    
    
    
    
    
    
    
}
-(void)showOrHidden:(UIButton *)btn
{
    [self.view endEditing:YES];
    btn.selected=!btn.selected;
    if (btn.selected) {
        _passTF.secureTextEntry=NO;
    }else
    {
        _passTF.secureTextEntry=YES;
    }
}
-(void)judgeCondition:(QCCountdownButton *)btn
{
    NSString *tel = _phoneTF.text;
    
    if([NSString isNULLString:tel]){
        
        [self.view makeToast:@"请输入手机号码"];
        [btn stopTime];
        
    }else
    {
        [self requestYZM:tel];
    }
    
    
    
    
}
#pragma mark 获取验证码请求
-(void)requestYZM:(NSString *)str
{
    NSDictionary * postDic;
    
    NSString *url=[NSString stringWithFormat:@"%@/app/user/generateSMSCode",APPBaseUrl];
    
    [NetworkPath requestWithMethod:0 andURLString:url andParameters:postDic  RequestSuccess:^(NSURLResponse *response, id responseObject)
     {
         LRLog(@"responseObject\n%@",responseObject);
         NSString *code = responseObject[@"code"];
         //如果code不存在，会不会崩溃
         if([code isEqualToString:@"10008"]){
             
             [self.view makeToast:@"验证码已发送"];
         }else{
             [_codeBtn stopTime];
             NSString *str=responseObject[@"msg"];
             
             [self.view makeToast:str];
         }
     } RequestFailed:^(NSError *error) {
         [_codeBtn stopTime];
         
         [self.view makeToast:NetError];
     }];
}
@end
